//
//  WTUserInfoService.m
//  WTLiveSDKDemo
//
//  Created by ben on 16/10/7.
//  Copyright © 2016年 ben. All rights reserved.
//

#import "WTUserInfoService.h"

@implementation WTUserInfoService
- (void)start:(NSDictionary *)param completionHandler:(WTServiceCompletionHandler)completionHandler{
    
    self.subPath = @"/index/private";
    self.param = param;
    NSString* cacheKey = [NSString stringWithFormat:@"%@",param[@"net_ease_or_user_id"]];
    id  cache = [[WTCacheManager shareManager] objectForKey:cacheKey];
    if(cache){
        WTUserInfoModel* userInfoModel = [[WTUserInfoModel alloc] initWithUserInfo:cache];
        completionHandler(userInfoModel,nil);
    }
    [HYBNetworking postWithUrl:self.subPath refreshCache:YES params:@{@"hash":[self encode]} success:^(id response) {
        
        NSInteger code = [response[@"code"] integerValue];
        if(code == 200){

            WTUserInfoModel* userInfoModel = [[WTUserInfoModel alloc] initWithUserInfo:response[@"hash"]];
            completionHandler(userInfoModel,nil);
            [[WTCacheManager shareManager] save:response[@"hash"] key:cacheKey];
        }else {
            completionHandler(nil,nil);
        }
        
    } fail:^(NSError *error) {
        NSString* cacheKey = [NSString stringWithFormat:@"%@",param[@"net_ease_or_user_id"]];
        id  cache = [[WTCacheManager shareManager] objectForKey:cacheKey];
        if(cache){
            WTUserInfoModel* userInfoModel = [[WTUserInfoModel alloc] initWithUserInfo:cache];
            completionHandler(userInfoModel,nil);
        }else {
            completionHandler(nil,nil);
        }
    }];
    
}
@end
